package com.beici.waterghost.controller;

import com.beici.waterghost.except.authenExcept;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.model.user;
import com.beici.waterghost.service.collentService;
import com.beici.waterghost.service.impl.collentServiceImpl;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/collent")
@CrossOrigin
public class collentController {

    @Autowired
    private collentServiceImpl collentService;
    // 获取用户收藏的所有帖子
    @GetMapping("/usercollents")
    public resultes userCollents(){
        try {
            user principal = (user) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Integer userId = principal.getUserId();
            return collentService.userCollentNodes(userId);
        }catch (NullPointerException e){
            throw new authenExcept("请登录在进行操作");
        }
    }

    @PutMapping("/collentnode/{nodeId}/{state}")
    public resultes collentNode(@PathVariable("nodeId") Long nodeId,@PathVariable("state") Integer state){
            user principal = (user) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Integer userId = principal.getUserId();
            return collentService.collentOrEscNode(userId,nodeId,state);

    }
}
